//
//  DDMainVC.swift
//  SinaMicroBlog
//
//  Created by dqw on 15/7/28.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class DDMainVC: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        addChildVCs()
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        setupComposedButton()
    }
    private func setupComposedButton()
    {
        let W = tabBar.bounds.width / CGFloat(viewControllers!.count)
        let rect = CGRect(x: 0, y: 0, width: W, height: tabBar.bounds.height)
        composedButton.frame = CGRectOffset(rect, 2 * W, 0)
        tabBar.addSubview(composedButton)
    }
    
    lazy private var composedButton:UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "composeStatus", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return button
    }()
    
    func composeStatus() {
        
        let vc = DDSendStatusVC()
        let nav = UINavigationController(rootViewController: vc)
        
        presentViewController(nav, animated: true, completion: nil)
    }
    
    private func addChildVCs(){
        addChildVC(DDHomeVc(), title: "首页", imgName: "tabbar_home")
        addChildVC(DDMessageVC(), title: "信息", imgName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildVC(DDDiscoveryVC(), title: "发现", imgName: "tabbar_discover")
        addChildVC(DDProfileVc(), title: "我", imgName: "tabbar_profile")
        
    }
    private func addChildVC(VC:UIViewController, title:String, imgName:String){
        
        VC.title = title
        VC.tabBarItem.image = UIImage(named: imgName)
        tabBar.tintColor = UIColor.orangeColor()
        let NAV = UINavigationController(rootViewController: VC)
        addChildViewController(NAV)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
